package com.csun.cmny;

import cn.jpush.api.JPushClient;
import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.resolver.CmnyLocaleResolver;
import com.csun.cmny.provider.utils.unionpay.sdk.SDKConfig;
import com.csun.cmny.util.ApplicationContextUtil;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.Locale;
import java.util.Properties;

@Slf4j
@EnableRetry
@EnableCaching
@EnableHystrix
@EnableSwagger2
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableTransactionManagement
public class CmnyProviderOpcApplication implements CommandLineRunner {

	@Resource
	private CmnyProperties cmnyProperties;

	public static void main(String[] args) {
		SpringApplication.run(CmnyProviderOpcApplication.class, args);
		log.info("cmny provider opc application start...");
	}
	/**
	 * Get kaptcha bean default kaptcha.
	 *
	 * @return the default kaptcha
	 */
	@Bean(name = "captchaProducer")
	public DefaultKaptcha getKaptchaBean() {
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		properties.setProperty("kaptcha.border", "yes");
		properties.setProperty("kaptcha.border.color", "105,179,90");
		properties.setProperty("kaptcha.textproducer.font.color", "blue");
		properties.setProperty("kaptcha.image.width", "125");
		properties.setProperty("kaptcha.image.height", "45");
		properties.setProperty("kaptcha.session.key", "code");
		properties.setProperty("kaptcha.textproducer.char.length", "4");
		properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}

	@Bean
	public LocaleResolver localeResolver() {
		return new CmnyLocaleResolver();
	}

	@Bean
	public ReloadableResourceBundleMessageSource messageSource() {
		Locale.setDefault(Locale.CHINA);
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setDefaultEncoding("UTF-8");
		messageSource.setBasenames("i18n/errors/ErrorMessages", "i18n/prompt/PromptMessages",
				"i18n/validation/ValidationMessages", "classpath:ValidationMessages",
				"classpath:ErrorCodeMessages");
		return messageSource;
	}

	@Bean
	public Validator getValidator() throws Exception {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource());
		return validator;
	}

	@Bean
	public CmnyLocaleResolver cmnyLocaleResolver() {
		return new CmnyLocaleResolver();
	}

	@Bean
	public ApplicationContextUtil applicationContextUtil() {
		return new ApplicationContextUtil();
	}


	@Bean(name = "cmnyFamily")
	public JPushClient getCmnyFamily() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
				cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return jPushClient;
	}

	/**
	 * 极光推送 -- 社区版 - 中控台
	 */
	@Bean(name = "cmnyTablet")
	public JPushClient getCmnyTablet() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		JPushClient jPushClient = new JPushClient("b476e81b8f0bc009a21c6160","c6bf60546c44001483914962");

		return jPushClient;
	}

	/**
	 * 极光推送 -- Nursing
	 */
	@Bean(name = "nursing")
	public JPushClient getNursing() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("a440b5327b0d34034fb57de1", "c7fb8f8f3d534cb2a2aefdf2");
	}

	/**
	 * 极光推送 -- 守护佳-社区版-中控台
	 */
	@Bean(name = "shouhjCmnyTablet")
	public JPushClient getShouhjCmnyTablet() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("ae148d197b1113daa09efbce","8d7e2e135999ce6a7f5116aa");
	}

	/**
	 * 极光推送 -- 守护佳-社区版-家属App
	 */
	@Bean(name = "shouhjFamily")
	public JPushClient getShouhjFamily() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("09627f5947f50c3457b28998", "481e2d18ed9d36bfab31670c");
	}

	/**
	 * 极光推送 -- Nursing
	 */
	@Bean(name = "shouhj")
	public JPushClient getShouhj() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("9217f07c6af4b74a7ef5ce01","8414c29065a6d92d210e2d22");
	}

	/**
	 * 极光推送 -- Nursing
	 */
	@Bean(name = "shouhjTablet")
	public JPushClient getShouhjTablet() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("2503ab9323ec061e92dff702","a21d3dd8d10278715a9cfca1");
	}

	/**
	 * 极光推送 -- 守护佳-志愿者
	 */
	@Bean(name = "shouhjVolunteer")
	public JPushClient getShouhjVolunteer() {
//        JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
//                cmnyProperties.getJpush().getCmnyFamily().getAppKey());

		return new JPushClient("02c4119544f9abd1bfa2bc7d", "624d132fd51a4a0bf5fec32e");
	}


	@Override
	public void run(String... args) throws Exception {

		SDKConfig.getConfig().loadPropertiesFromSrc();
	}

}
